﻿using InParkRedemptionAPI.DataAccess;
using InParkRedemptionAPI.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Internal;
using RapidSalesAdminPortalAPI.DataAccess;
using WebAPI.Common;
using WebAPI.Controllers;
using WebAPI.Models;
using WebAPI.Util;

namespace InParkRedemptionAPI.Controllers
{
    [Authorize]
    [Route("api/[controller]")]
    [ApiController]
    public class ActivityLogController : ParentController
    {
        private readonly RS_AdmPortalContext _db;
        private readonly ILogUtility _logUtility;
        private readonly ILogger _logger;
        private readonly IDataProtectorUtility _dataProtectorUntility;
        public ActivityLogController(RS_AdmPortalContext db, IConfiguration configuration, ILogUtility logUtility, ILogger<ActivityLogController> logger, IDataProtectorUtility dataProtectorUntility)
        {
            _db = db;
            _logUtility = logUtility; // sample code for DI of common utility class
            _logger = logger;
            _dataProtectorUntility = dataProtectorUntility;
        }

        //[HttpPost("ActivityLogList")]
        //public async Task<ActionResult<ResultMsg>> ActivityLogList([FromBody] ActivityLogListRequest requestModel)
        //{
        //    var resultMsg = new ResultMsg();
        //    try
        //    {
        //        //var paComont = _db.ActivityLogs.Join(_db.FrIpRedemptionAccounts,
        //        //            a => a.RedemptionAccount,
        //        //            b => b.AccountId,
        //        //            (a, b) => new { a, b })
        //        //    .Join(_db.FrIpRedemptionPoints,
        //        //              o => o.a.RedemptionPoint,
        //        //              c => c.Id,
        //        //              (o, c) => new { o, c })
        //        //    .Join(_db.FrIpRedemptionPointProducts,
        //        //            p => p.o.a.RedemptionProduct,
        //        //            d => d.ProductId,
        //        //            (a, d) => new { a, d })
        //        //    .Select(x=> new ActivityLogListResponse()
        //        //    {
        //        //        ActivityLogId = x.a.o.a.ActivityLogId,
        //        //        RedemptionDate = x.a.o.a.RedemptionDate,
        //        //        //RedemptionAccount = x.a.o.b.StaffName,
        //        //        //RedemptionPoint = x.a.c.Name,
        //        //        //RedemptionProduct = x.d.Product.DefaultSetValue,
        //        //        TicketIdentificationCode = x.a.o.a.TicketIdentificationCode,
        //        //        RedemptionProductQuantity = x.a.o.a.RedemptionProductQuantity.ToString(),
        //        //    }).AsQueryable();


        //        var paComont = _db.ActivityLogs.GroupJoin(_db.FrIpRedemptionAccounts,
        //                    a => a.RedemptionAccount,
        //                    b => b.AccountId,
        //                    (a, b) => new { a, b })
        //            .SelectMany(x=> x.b.DefaultIfEmpty(), (x,b) => new { x.a, b})

        //            .GroupJoin(_db.FrIpRedemptionPoints,
        //                      x => x.a.RedemptionPoint,
        //                      c => c.Id,
        //                      (x, c) => new { x.a, x.b, c })
        //             .SelectMany(x => x.c.DefaultIfEmpty(), (x, c) => new { x.a, x.b, c })

        //            .GroupJoin(_db.FrIpDataDictionaries,
        //                    x => x.a.RedemptionProduct,
        //                    d => d.Id,
        //                    (x, d) => new { x.a, x.b, x.c, d})
        //              .SelectMany(x => x.d.DefaultIfEmpty(), (x, d) => new { x.a, x.b, x.c, d})

        //            .Select(x => new ActivityLogListResponse()
        //            {
        //                ActivityLogId = x.a.ActivityLogId,
        //                RedemptionDate = x.a.RedemptionDate,
        //                RedemptionAccount = x.b.StaffName,
        //                RedemptionPoint = x.c.Name,
        //                RedemptionProduct = x.d.DefaultSetValue,
        //                TicketIdentificationCode = x.a.TicketIdentificationCode,
        //                RedemptionProductQuantity = x.a.RedemptionProductQuantity.ToString(),
        //            }).AsQueryable();

        //        if (!string.IsNullOrWhiteSpace(requestModel.StartDate))
        //            paComont = paComont.Where(x => x.RedemptionDate > Convert.ToDateTime(requestModel.StartDate));
        //        if (!string.IsNullOrWhiteSpace(requestModel.EndDate))
        //            paComont = paComont.Where(x => x.RedemptionDate < Convert.ToDateTime(requestModel.EndDate));
        //        if (!string.IsNullOrWhiteSpace(requestModel.RedemptionAccount))
        //            paComont = paComont.Where(x => x.RedemptionAccount.Contains(requestModel.RedemptionAccount));
        //        if (!string.IsNullOrWhiteSpace(requestModel.RedemptionPoint))
        //            paComont = paComont.Where(x => x.RedemptionPoint.Contains(requestModel.RedemptionPoint));
        //        if (!string.IsNullOrWhiteSpace(requestModel.RedemptionProduct))
        //            paComont = paComont.Where(x => x.RedemptionProduct.Contains(requestModel.RedemptionProduct));
        //        if (!string.IsNullOrWhiteSpace(requestModel.TicketIdentificationCode))
        //            paComont = paComont.Where(x => x.TicketIdentificationCode.Contains(requestModel.TicketIdentificationCode));
        //        if (!string.IsNullOrWhiteSpace(requestModel.RedemptionProductQuantity))
        //            paComont = paComont.Where(x => x.RedemptionProductQuantity.Contains(requestModel.RedemptionProductQuantity));


        //        if (!string.IsNullOrWhiteSpace(requestModel.Field) && paComont.Count() > 0)
        //            paComont = DBHelper.DataSorting(paComont, requestModel.Field, requestModel.Order);
        //        else
        //            paComont = DBHelper.DataSorting(paComont, "RedemptionDate", "desc");

        //        var sysLogList = await BasePageOutputExtensions.ToPagedListAsync(paComont.ToList(), requestModel.Page, requestModel.PageSize);

        //        return resultMsg.SetResultMsg(
        //             (int)ResultCode.Success,
        //             HelperUtility.GetDescription(ResultCode.Success),
        //             sysLogList);
        //    }
        //    catch (Exception ex)
        //    {
        //        _logger.LogError(ex.Message);
        //        return resultMsg.SetResultMsg(
        //             (int)ResultCode.ErrorOnRequestUnMarshalling,
        //             HelperUtility.GetDescription(ResultCode.ErrorOnRequestUnMarshalling),
        //             null);
        //    }
        //}
    }
}
